package com.crowd.web.controller;

import com.crowd.constant.CrowdConstant;
import com.crowd.entity.Admin;
import com.crowd.entity.Student;
import com.crowd.service.api.IAdminService;
import com.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Stone
 * @date 2020/4/27 16:54
 */
@Controller
public class AdminController {
    @Autowired
    private IAdminService adminService;

    public Logger logger = LoggerFactory.getLogger(AdminController.class);
    
    /**
     * @Author:Stone
       * @Param loginAcct: 
     * @Param userPswd: 
     * @Param session:
       * @return: java.lang.String
     * @Date: 2020/5/13 18:00
     * @Description:登录请求
    */
    @RequestMapping("/admin/do/login")
    public String doLogin(@RequestParam("loginAcct") String loginAcct,
                          @RequestParam("userPswd") String userPswd,
                          HttpSession session){
        // 调用Service 方法执行登录检查
        // 这个方法如果能够返回admin 对象说明登录成功，如果账号、密码不正确则会抛出异常
        Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd);
        // 将登录成功返回的admin 对象存入Session 域
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);

        //如果是用转发方式，
        // http://localhost:8080/crowdfunding02_admin_webui_war_exploded/admin/do/login.html?loginAcct=stone&userPswd=123456
        //当刷新页面时，会重复提交表单，而且参数会全部显示在url上，所以要用重定向
        //return "/admin-main";
        return "redirect:/admin/to/main/page.html";
    }

    /**
     * @Author:Stone

       * @return: java.lang.String
     * @Date: 2020/6/4 14:38
     * @Description: springsecurity中HttpSecurity.successForwardUrl()方法一定要是post请求
     * 所以把<mvc:view-controller></mvc:view-controller>的跳转改成PostMapping
    */
    @PostMapping("/admin/to/main/page.html")
    public String toLoginPage(){
        return "/admin-main";
    }

    /**
     * @Author:Stone
       * @Param session:
       * @return: java.lang.String
     * @Date: 2020/5/9 15:00
     * @Description:登出请求
    */
    @RequestMapping("/admin/do/logout")
    public String doLogout(HttpSession session){
        //session.removeAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);
        //使session失效，跟关闭浏览器一样效果
        session.invalidate();
        return "redirect:/admin/to/login/page.html";
    }

    /**
     * @Author:Stone
       * @Param keyword:
     * @Param pageNum:
     * @Param pageSize:
     * @Param model:
       * @return: java.lang.String
     * @Date: 2020/5/11 17:20
     * @Description:通过keyword获取用户列表，通过2分页的方式
    */
    @RequestMapping("/admin/get/page")
    public String getAdminPage(@RequestParam(value = "keyword",defaultValue = "") String keyword,
                              @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "5") int pageSize,
                              Model model){
        PageInfo<Admin> adminPageInfo = adminService.getAdminPage(keyword, pageNum,pageSize );
        model.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, adminPageInfo);
        return "/admin-page";
    }

    /**
     * @Author:Stone
       * @Param id:
       * @return: java.lang.String
     * @Date: 2020/5/13 17:28
     * @Description:根据id删除用户
    */
    @RequestMapping("/admin/remove/{id}/{pageNum}/{keyword}")
    public String remove(@PathVariable(value = "id") Integer id,
                         @PathVariable(value = "pageNum") Integer pageNum,
                         @PathVariable(value = "keyword") String keyword){
        adminService.remove(id);
        return "redirect:/admin/get/page.html"+"?pageNum="+pageNum+"&keyword="+keyword;
    }

    /**
     * @Author:Stone
       * @Param null:
       * @return: null
     * @Date: 2020/5/16 13:08
     * @Description:新增用户操作
    */
    // 在开启@EnableGlobalMethodSecurity(prePostEnabled = true)后生效
    @PreAuthorize("hasAuthority('user:save')")
    @RequestMapping("/admin/save")
    public String save(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/get/page.html?pageNum="+Integer.MAX_VALUE;
    }

    /**
     * @Author:Stone
       * @Param id:
     * @Param model:
       * @return: java.lang.String
     * @Date: 2020/5/16 14:10
     * @Description:跳转到修改页面
    */
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam(value = "id") Integer id,Model model){
        Admin admin = adminService.findByPrimaryKey(id);
        model.addAttribute("admin", admin);
        return "/admin-edit";
    }

    /**
     * @Author:Stone
       * @Param admin:
     * @Param pageNum:
     * @Param keyword:
       * @return: java.lang.String
     * @Date: 2020/5/16 14:26
     * @Description:更新用户信息
    */
    @RequestMapping("/admin/update")
    public String update(Admin admin,
                         @RequestParam(value = "pageNum") Integer pageNum,
                         @RequestParam(value = "keyword") String keyword){
        adminService.update(admin);
        return "redirect:/admin/get/page.html"+"?pageNum="+pageNum+"&keyword="+keyword;
    }

    /**
     * @Author:Stone
       * @Param numList:
       * @return: com.crowd.util.ResultEntity<java.util.List<java.lang.Integer>>
     * @Date: 2020/6/6 17:23
     * @Description: 测试@PreFilter用法，使用postman测试
    */
    @PreFilter(value = "filterObject%2==0")
    @ResponseBody
    @RequestMapping("/admin/test/pre/filter.json")
    public ResultEntity<List<Integer>> filterList(@RequestBody List<Integer> numList){
        return ResultEntity.successWithData(numList);
    }

}
